package com.cloud.example.business.flow.controller.depend;

import com.cloud.async.flow.web.client.FlowTemplate;
import com.cloud.async.flow.wrapper.WorkerWrapper;
import com.cloud.example.business.flow.controller.depend.dependnew.worker.DeWorker;
import com.cloud.example.business.flow.controller.depend.dependnew.worker.DeWorker1;
import com.cloud.example.business.flow.controller.depend.dependnew.worker.DeWorker2;
import com.cloud.example.business.flow.controller.depend.dependnew.worker.User;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutionException;

/**
 * 后面请求依赖于前面请求的执行结果
 * 且在类中get依赖参数
 *
 * @author chengrui
 */
@RequestMapping("/api/flow")
@RestController
public class DependNewController {

    /**
     * logger
     */
    private final Log logger = LogFactory.getLog(getClass());

    @Autowired
    FlowTemplate flowTemplate;

    @GetMapping("/dependNew")
    public String dependNew() throws ExecutionException, InterruptedException {

        DeWorker w = new DeWorker();
        DeWorker1 w1 = new DeWorker1();
        DeWorker2 w2 = new DeWorker2();

        WorkerWrapper<User, String> workerWrapper2 =  new WorkerWrapper.Builder<User, String>()
                .worker(w2)
                .callback(w2)
                .id("third")
                .build();

        WorkerWrapper<String, User> workerWrapper1 = new WorkerWrapper.Builder<String, User>()
                .worker(w1)
                .callback(w1)
                .id("second")
                .next(workerWrapper2)
                .build();

        WorkerWrapper<String, User> workerWrapper = new WorkerWrapper.Builder<String, User>()
                .worker(w)
                .param("Depend New param")
                .id("first")
                .next(workerWrapper1)
                .callback(w)
                .build();

        //不用给wrapper setParam了，直接在worker的action里自行根据id获取即可

        flowTemplate.startWork(2222, workerWrapper);

        return "success!";
    }


}
